define(function(require){
	var main = require('/static/main.js');
	var m = new main();
	function Cont(){
		this.Fun._init();
	}
	Cont.prototype.Fun = {
		_init:function(){
		    this.getSign();
		    this.getnum('',0);
		    this.getGroup();
			this.bindEvent();
		},

		bindEvent:function(){
			var _this = this;
			$('.btn-info').click(function(){
				var param = {};
				var Content = $('.form-controltextarea').val();
				var SendType = parseInt($(this).attr('SendType'));
				if($.trim(Content) == ''){
					layer.msg('请填写要发送的内容');
					return false;
				}
				if($.trim(Content).length > 140){
					layer.msg('短信数字在250以内');
					return false;
				}
				if(SendType == 300){
	                var TagIds = [];
	                $('.news_choseallsign .Label_btnspan.E8ECEE').each(function(k,v){
	                    var id = $(this).data('id')
	                    TagIds.push(id);
	                })
	                param.TagIdList = TagIds;
	                if(param.TagIdList.length <= 0 ){
	                	layer.msg('标签不能为空');
						return false;
	                }
				}
				if(SendType == 200){
	                var PhoneList = [];
	                $('#tags_tagsinput span.tag span').each(function(k,v){
						var phone = $(this).text().replace(/\s/g, "");
	                    PhoneList.push(phone);
					})
	                param.PhoneList = PhoneList;
	                if(param.PhoneList.length <= 0 ){
	                	layer.msg('请添加手机号');
						return false;
	                }
				}
				if(SendType == 400){
	                var GroupList = [];
	                $('.newsmodel_signeach .Label_btnspan.E8ECEE').each(function(k,v){
	                    var id = $(this).data('id')
	                    GroupList.push(id);
	                })
	                param.GroupList = GroupList;
	                if(param.GroupList.length <= 0 ){
	                	layer.msg('请至少选择一个分组');
						return false;
	                }
				}

				param.Content = Content;
				param.SendType = SendType;


				layer.confirm('确定发送改短信，发送成功后即无法撤回？', {
				  btn: ['确定','取消'] //按钮
				}, function(){
					$.ajax({
		                type: "POST",
		                data:param,
		                url: "/news/create",
		                dataType: "json",
						success:function (ret) {
			               if(ret.IsBizSuccess){
			               		layer.msg('发送成功');
			               		// setTimeout(function(){
			               		// 	window.location.href = "/news/newslist"
			               		// },1000)
			               }else{
			               		layer.msg(ret.ErrMsg);
			               }
	     					layer.close();
			            }
		        	})
				}, function(){
				 
				});
				
			})
			// 点击选择标签
			$(document).on('click','.news_choseallsign .Label_btn span',function(){
				if($(this).hasClass('E8ECEE')){
					$(this).removeClass('E8ECEE')
				}else{
					$(this).addClass('E8ECEE')
				}
				var TagIds = [];
                $('.news_choseallsign .Label_btnspan.E8ECEE').each(function(k,v){
                    var id = $(this).attr('data-id')
                    TagIds.push(id);
                })
                var data = {
                	Ids:TagIds,
                }
                if(TagIds.length > 0 ){
                	_this.getnum(data,1);
                }else{
                	$('.news_choseallsign .chose_count').html(0);
                }
			})
			// 计算 数量
			var changeNum = function(obj,type){
				// // 标签
				// if(type == 100){
				// 	var tot = 0;
				// 	$('.news_choseallsign .Label_btn span').each(function(k,v){
				// 		if($(this).hasClass("E8ECEE")){
				// 			tot++;
				// 		}
				// 	})
				// 	$(obj+' .chose_count').html(tot);
				// }
				// 添加手机号
				if(type == 200){
					var num = 0;
					$('#tags_tagsinput span.tag').each(function(k,v){
						num++;
					})
					$(obj+' .chose_count').html(num);
				}

			}
			// 选择模板事件
			// 调用个 ajax  改变val内容
			$('.chose_newsmodel').change(function(){
				var val = $(this).val();
				$('.form-controltextarea').val(val);
			})

			// 点击添加 手机号
			$('.input-group-btn button').click(function(){
				var cont = $('#example-input1-group2').val().replace(/\s/g, "");
				if(cont == ''){
					layer.msg('手机号不能为空');
					return false;
				}
				if(!m.IsPhone(cont)){
					layer.msg('手机格式不正确');
					return false;
				}
				var ishave = false;
				$('#tags_tagsinput span.tag span').each(function(k,v){
					var phone = $(this).text().replace(/\s/g, "");
					if(cont == phone){
						ishave = true;
					}
				})
				if(ishave){
					layer.msg('已添加');
					return false
				}
				$('#tags_tagsinput').append('<span class="tag"><span>'+cont+'</span><i>x</i></span>');
				changeNum('.phone_chose',200);
			})
			$(document).on('click','div.tagsinput span.tag i',function(){
				$(this).parent('.tag').remove();
				changeNum('.phone_chose',200);
			})
			// 点击选择分组
			$(document).on('click','.group_list .Label_btn span',function(){
				if($(this).hasClass('E8ECEE')){
					$(this).removeClass('E8ECEE')
				}else{
					$(this).addClass('E8ECEE')
				}
				var TagIds = [];
                $('.group_list .Label_btnspan.E8ECEE').each(function(k,v){
                    var id = $(this).attr('data-id')
                    TagIds.push(id);
                })
                var data = {
                	Ids:TagIds,
                }
                if(TagIds.length > 0 ){
                	_this.getnum(data,2);
                }else{
                	$('.group_list .chose_count').html(0);
                }
				// changeNum('.group_list',100);
			})
		},
		getnum:function(data,ReqType){
			if(!data){
				var data = {};
			}
			data.ReqType = ReqType
			$.ajax({
                type: "POST",
                url: "/news/get-num",
                dataType: "json",
                data:data,
                success: function (ret) {
                   if(ret.IsBizSuccess){
                   		var obj = '';
	            	   if(ReqType == 1){
	            	   		obj = '.news_choseallsign'
	            	   }else if(ReqType == 2){
	            	   		obj = '.group_list';
	            	   }else if(ReqType == 0){
	            	   		obj = '.all';
	            	   }
	            	   $(obj+' .chose_count').html(ret.TotalCounts);
                   }else{
                   		layer.msg(ret.ErrMsg);
						return false
                   }

                }
            });
		},
		getSign:function(){
			$.ajax({
                type: "POST",
                url: "/customer/sign-list",
                dataType: "json",
                success: function (ret) {
            	   var str = '';
            	   if(ret.Models.length > 0){
        	   			$.each(ret.Models,function(k,v){
	            	   		str+='<span class="Label_btnspan" data-id="'+v.TagId+'" data-name="'+v.TagName+'">'+v.TagName+'</span>'
	            	    })
            	   	   $('.news_choseallsign .Label_btn').html(str);
            	   }else{
            	   		$('.news_choseallsign').hide();
            	   }
                }
            });
		},
		// 分组列表
		getGroup:function(){
			$.ajax({
                type: "POST",
                url: "/customer/group-list",
                dataType: "json",
				success:function (ret) {
	               if(ret.IsBizSuccess){
		               	if(ret.Models.length > 0){
		               		var str = '';
		               		ret.Models.forEach(function(v,k){
		               			str += '<span class="Label_btnspan" data-id="'+v.GroupId+'" >'+v.GroupName+'</span>';
		               		})
		               		$('.group_list .Label_btn').html(str);
		               	}else{
		               		$('.group_list').hide();
		               	}
	               }
	            }
        	})
		},


	}
	var a = new Cont();

})